package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.user.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bouncycastle.crypto.macs.CMacWithIV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/2/26 17:41
 */
@Service
public class CartAsyncServiceImpl implements CartAsyncService {

    @Autowired
    private CartInfoMapper cartInfoMapper;


    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {

        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id", cartInfo.getUserId()).eq("sku_id", cartInfo.getSkuId());
        cartInfoMapper.update(cartInfo, cartInfoQueryWrapper);
    }

    @Override
    @Async
    public void saveCartInfo(CartInfo cartInfo) {
        cartInfoMapper.insert(cartInfo);
    }

    @Override
    @Async
    public void deleteCartInfo(String userTempId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id", userTempId));
    }

    @Override
    @Async
    public void checkCart(String userId, Long skuId, Integer isChecked) {
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id", userId).eq("sku_id", skuId);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(isChecked);

        //更新数据库
        cartInfoMapper.update(cartInfo, cartInfoQueryWrapper);

    }

    @Override
    @Async
    public void deleteCartInfo(String userId, Long skuId) {
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id", userId).eq("sku_id", skuId);
        cartInfoMapper.delete(cartInfoQueryWrapper);
    }


}
